package com.niren.userservice.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.niren.commonmodule.basic.EUseSystem;
import com.niren.commonmodule.entity.SysDeviceInfo;
import com.niren.commonmodule.util.JwtTokenUtils;
import com.niren.commonmodule.util.supportclass.UserTokenInfo;
import com.niren.userservice.feignClient.DeviceServiceFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * \* author: wky
 * \* Date: 2024/11/21
 * \* Time: 10:23
 * \* Project: microservice
 * \* Package: com.niren.userservice.controller
 * \* Version: 1.0.0
 * \* To change this template use File | Settings | File Templates.
 * \* Description:请输入源文件描述
 * \
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private DeviceServiceFeignClient deviceServiceFeignClient;

    @GetMapping("/test")
    public String test() {
        return "this is user service!";
    }

    /**
     * 测试访问连接 http://localhost:9081/user/adddevinfo?devSn=5465
     * @param devSn
     * @return
     */
    @RequestMapping("/adddevinfo")
    public String addDevInfo(@RequestParam(name = "devSn") String devSn) {
        SysDeviceInfo dev = new SysDeviceInfo();
        dev.setDevName("设备名" + devSn);
        dev.setDevSn(devSn);
        return deviceServiceFeignClient.addDevInfo(dev);
    }

    @RequestMapping("/login")
    public Object login(
            @RequestParam(name = "userName") String userName,
            @RequestParam(name = "password") String password
    ) {

        //验证用户名与密码的正确性
        //生成token
        UserTokenInfo info = new UserTokenInfo();
        info.setUserName(userName);
        info.setUserId(10);
        String token = JwtTokenUtils.createToken(info, EUseSystem.EUseSystem2Int(EUseSystem.PC));
        //将token写入到redis
        redisTemplate.opsForValue().set("token:" + token, token,60, TimeUnit.SECONDS);
        //将生成的token返回给前端
        return token;
    }

    @RequestMapping("/timeout")
    public Object timeout() {
        try{
            TimeUnit.SECONDS.sleep(1);
        }catch (Exception e) {}
        return "timeout ok";
    }

    @RequestMapping("/exception")
    public Object exception() {
        int i = 10 / 0;

        return "exception OK";
    }

    //热点限流测试
    @RequestMapping("/hotkeytest")
    @SentinelResource(value = "hotkeytest",blockHandler = "hotkeyBlockHandler")
    public Object hotKeyTest(
            @RequestParam(value = "p1",required = false) String p1,
            @RequestParam(value = "p2",required = false) String p2
    ) {

        return "hotKeyTest OK";
    }


    public Object hotkeyBlockHandler(
            String p1, String p2, BlockException e
            ) {

        return "hotKeyTest，触发热点限流！";
    }
}
